'use strict';

const { Controller } = require('egg');

class Dati extends Controller {
    /**
     * get
     * 抽奖接口
     */
    async choujiang() {
        // 读取奖品表数据
        const res_total = await this.app.mysql.get('youanmi_dati');
        const res_used = await this.app.mysql.get('youanmi_dati', { id: 2 }) || {};
        const now_day = dateFormat('YYYY/mm/dd', new Date());
        if (res_total.updated_at !== now_day) {
            await this.app.mysql.beginTransactionScope(async conn => {
                const resetOldData = {
                    yideng: Number(res_total.yideng) - 1 > 0 ? Number(res_total.yideng) - 1 : 0,
                    erdeng: Number(res_total.erdeng) - 2 > 0 ? Number(res_total.erdeng) - 2 : 0,
                    sandeng: Number(res_total.sandeng) - 4 > 0 ? Number(res_total.sandeng) - 4 : 0,
                    canyu: Number(res_total.canyu) - 40 > 0 ? Number(res_total.canyu) - 40 : 0,
                    updated_at: now_day
                };
                const resetUsedData = {
                    yideng: Number(res_total.yideng) - 1 > 0 ? Number(res_used.yideng) + 1 : Number(res_used.yideng) + Number(res_total.yideng),
                    erdeng: Number(res_total.erdeng) - 2 > 0 ? Number(res_used.erdeng) + 2 : Number(res_used.erdeng) + Number(res_total.erdeng),
                    sandeng: Number(res_total.sandeng) - 4 > 0 ? Number(res_used.sandeng) + 4 : Number(res_used.sandeng) + Number(res_total.sandeng),
                    canyu: Number(res_total.canyu) - 40 > 0 ? Number(res_used.canyu) + 40 : Number(res_used.canyu) + Number(res_total.canyu),
                    updated_at: now_day
                };
                // console.log(resetOldData, resetUsedData);
                await conn.update('youanmi_dati', resetOldData, { where: { id: 1 }});
                await conn.update('youanmi_dati', resetUsedData, { where: { id: 2 }});
            }, this.ctx);
        }
        const res_used_roll = await this.app.mysql.get('youanmi_dati', { id: 2 }) || {};
        const arr_roll = [];
        let total_num = 0;
        // 装入一等奖
        for (let i = 0; i < res_used_roll.yideng; i++) {
            arr_roll.push('yideng');
            total_num++;
        }
        // 装入二等奖
        for (let i = 0; i < res_used_roll.erdeng; i++) {
            arr_roll.push('erdeng');
            total_num++;
        }
        // 装入三等奖
        for (let i = 0; i < res_used_roll.sandeng; i++) {
            arr_roll.push('sandeng');
            total_num++;
        }
        // 装入四等奖
        for (let i = 0; i < res_used_roll.canyu; i++) {
            arr_roll.push('canyu');
            total_num++;
        }
        const roll_box = [...arr_roll];
        // 装入谢谢参与
        // for (let i = 0; i < 100 - arr_roll.length; i++) {
        for (let i = 0; i < 200; i++) {
            roll_box.push('xiexie');
        }
        const num = Math.floor(Math.random() * (roll_box.length));
        console.log(num);
        if (num <= total_num) {
            const roll_data = {};
            if (Number(res_used_roll[arr_roll[num]]) - 1 > 0) {
                roll_data[arr_roll[num]] = Number(res_used_roll[arr_roll[num]]) - 1;
            }
            console.log(roll_data);
            await this.app.mysql.update('youanmi_dati', roll_data, { where: { id: 2 }});
        }
        return this.ctx.returnBody(200, '抽奖成功', { data: arr_roll[num] || roll_box[num] });
    }
    /**
     * get
     * 查询剩余奖项
    */
    async checkTotal() {
        const res_total = await this.app.mysql.get('youanmi_dati');
        const res_used = await this.app.mysql.get('youanmi_dati', { id: 2 }) || {};
        const total = {
            一等奖: Number(res_total.yideng) + Number(res_used.yideng),
            二等奖: Number(res_total.erdeng) + Number(res_used.erdeng),
            三等奖: Number(res_total.sandeng) + Number(res_used.sandeng),
            参与奖: Number(res_total.canyu) + Number(res_used.canyu)
        };
        return this.ctx.returnBody(200, '查询成功', { ...total });
    }
}

module.exports = Dati;

function dateFormat(fmt, date) {
    let ret;
    const opt = {
        'Y+': date.getFullYear().toString(), // 年
        'm+': (date.getMonth() + 1).toString(), // 月
        'd+': date.getDate().toString(), // 日
        'H+': date.getHours().toString(), // 时
        'M+': date.getMinutes().toString(), // 分
        'S+': date.getSeconds().toString() // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (const k in opt) {
        ret = new RegExp('(' + k + ')').exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
        }
    }
    return fmt;
}
